$(function(){
	setTextFieldFocus();
	setAddProjectListener();
	setPersonInChargeExistListener();
	setMemberExistListener();
	setSubmitListener();
});

function setTextFieldFocus(){
	$(".myProject .detail form input[type='text']")[0].focus();
}

function setAddProjectListener(){
	$(".myProject .addProject").click(function(){
		$(".myProject .addProjectInput").fadeIn("fast");
		$(".myProject .myProjectMask").fadeIn("fast");
		$("#projectName").focus();//@memory display:none 的元素.focus()是不起作用的
		$(".myProject .addProjectInput .adjustButton button[type='button']").click(function(){
			$(".myProject .addProjectInput").fadeOut("fast");
			$(".myProject .myProjectMask").fadeOut("fast");			
		});
		$(".myProject .myProjectMask").click(function(){
			$(".myProject .addProjectInput").fadeOut("fast");
			$(".myProject .myProjectMask").fadeOut("fast");			
		});
	});
}

function setMemberExistListener(){
	$("#addProjectMember").keyup(function(){

			var array = $(this).val().split(' ');

			for(var i = 0; i < array.length; i++){//array.length-1舍去末尾的空串
				if(!isExist(array[i])){
					if(array[i] != ''){
						$(".myProject .memberNotExist").get(0).innerHTML = array[i] + "：未被录入，或者你的输入不正确";
						$(".myProject .memberNotExist").fadeIn("fast");						
					}
				}
			}
			
			var j;
			for(j = 0; j < array.length; j++){
				if(!isExist(array[j])){
					break;
				}
			}
			
			if(j == array.length){
				$(".myProject .memberNotExist").fadeOut("fast");
			}
			
		if($("#addProjectMember").val().length == 0){
			$(".myProject .memberNotExist").fadeOut("fast");
		}
	});
	
}

function setPersonInChargeExistListener(){
	$("#projectPersonInCharge").keyup(function(){

			var array = $(this).val().split(' ');

			for(var i = 0; i < array.length; i++){//array.length-1舍去末尾的空串
				if(!isExist(array[i])){
					if(array[i] != ''){
						$(".myProject .personInChargeNotExist").get(0).innerHTML = array[i] + "：未被录入，或者你的输入不正确";
						$(".myProject .personInChargeNotExist").fadeIn("fast");						
					}
				}
			}
			
			var j;
			for(j = 0; j < array.length; j++){
				if(!isExist(array[j])){
					break;
				}
			}			
			if(j == array.length){
				$(".myProject .personInChargeNotExist").fadeOut("fast");
			}
			
		if($("#projectPersonInCharge").val().length == 0){
			$(".myProject .personInChargeNotExist").fadeOut("fast");
		}
	});

}


function isExist(value){
	var trueOrFalse = false;
	
	for(var j = 0; j < $(".myProject .memberHidden p").length; j++){
		if(value == $(".myProject .memberHidden p").get(j).innerHTML){
			trueOrFalse = true;
		}
	}
	
	return trueOrFalse;
}

function setSubmitListener(){
	$(".myProject .addProjectInput button[type='submit']").click(function(){
		if($("#projectName").val().trim().length == 0 ||
			$("#projectPersonInCharge").val().trim().length == 0 ||
			$("#addProjectMember").val().trim().length == 0){
				$(".myProject .addProjectInput .inputBlank").get(0).innerHTML = "所填写的项不能为空";
				$(".myProject .addProjectInput .inputBlank").fadeIn("fast");
				return false;
			}
		if(!$(".myProject .personInChargeNotExist").is(":hidden")||
			!$(".myProject .memberNotExist").is(":hidden")){
				return false;
			}
	});
	$("#projectName").focus(function(){
		$(".myProject .addProjectInput .inputBlank").fadeOut("fast");
	});
	$("#projectPersonInCharge").focus(function(){
		$(".myProject .addProjectInput .inputBlank").fadeOut("fast");
	});
	$("#addProjectMember").focus(function(){
		$(".myProject .addProjectInput .inputBlank").fadeOut("fast");
	});	
}
